package cn.bx.bid.ctrl;

import cn.bx.bid.entity.AjaxResult;
import cn.bx.bid.entity.Page;
import cn.bx.bid.entity.User;
import cn.bx.bid.entity.Userrole;
import cn.bx.bid.service.UserService;
import cn.bx.bid.util.LogUtil;
import cn.bx.bid.util.Md5Util;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import sun.security.provider.MD5;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
public class UserController {
   @Resource
   UserService userService;

   //用户登录
@RequestMapping("login1")
public AjaxResult login( @RequestParam("username") String username, @RequestParam("password") String pass){
  pass= Md5Util.getMD5(pass);
  User user=  userService.login(username,pass);
  if(user!=null){
      return AjaxResult.success("true",user);
  }

    return AjaxResult.error("false");
}
    //用户查询列表
    @GetMapping(value = "userSearch",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Page<User> search(@RequestParam(value = "page",defaultValue = "1") int pageNo, @RequestParam(value = "limit",defaultValue = "10") int limit, @RequestParam(value = "name",required = false) String name, @RequestParam(value = "startDate",required = false) Date start, @RequestParam(value = "endDate",required = false) Date end){
          pageNo--;
          org.springframework.data.domain.Page<User>  page=userService.paging(pageNo, limit, name, start, end);

        LogUtil.info("start:>>>>>>>>>>"+start);
        LogUtil.info("end:>>>>>>>>>>"+end);
        return Page.toPage(page);

    }
    @RequestMapping(value = "userAdd",produces = "application/json;charset=utf-8")
   // ,@RequestParam("role")long role
    //@RequestBody
    public AjaxResult userAdd(@RequestBody User user){
    Userrole userrole=new Userrole();
      userrole.setId(user.getRole());
    //   userrole.setId(role);
     user.setUserrole(userrole);
        System.out.println(">>>>>>>>user>>>>>>>"+user.toString());
       int a= userService.add(user);
        if(a==1){
            return AjaxResult.success("true");
        }

        return AjaxResult.error("false");
    }

    //查询修改用户信息
    @GetMapping(value = "userGet",produces = "application/json;charset=utf-8")
    @ResponseBody
    public  User userGet(@RequestParam("id")long id){
        User u=  userService.get(id);
        return u;

    }
    //更新用户信息
    @RequestMapping(value = "userUpdate",produces = "application/json;charset=utf-8")
    @ResponseBody
    //@ModelAttribute("u")
    public AjaxResult userUpdate(@RequestBody User user){
        Userrole userrole=new Userrole();
        userrole.setId(user.getRole());
        user.setUserrole(userrole);
    LogUtil.info(user+">>>>>>>>>>>>>>>>>>");

        int a= userService.updata(user);

        if(a>0){
            return AjaxResult.success("true");
        }
        return  AjaxResult.error("false");
    }
    //删除用户
    @RequestMapping("userdel")
    @ResponseBody
    public AjaxResult del1(HttpServletRequest request, @RequestParam("id") long id){
    User user=new User();
    user.setId(id);
        int u= userService.delete(user);
        if(u>0){
            return AjaxResult.success("删除成功");
        }
        return  AjaxResult.error("删除失败");
    }
    //批量删除
    @RequestMapping(value = "batchDeluser",produces = "application/json;charset=utf-8")
    @ResponseBody
    public AjaxResult batchDel(@RequestParam("ids[]")long[] ids){
        int b=  userService.batchDel(ids);
        return b>0?AjaxResult.success("删除成功"):AjaxResult.error("删除失败");
    }
    // checkUserNameRepeat
    @RequestMapping(value = "checkUserNameRepeat",produces = "application/json;charset=utf-8")
    public AjaxResult checkUserNameRepeat(@RequestParam("userName")String userName, @RequestParam(value = "id",required = false)Long id){
        boolean a= userService.checkUserNameRepeat(id,userName);
        if(a){
            return  AjaxResult.error("登录名重复");
        }
        return AjaxResult.success("登录名不存在");
    }


    //异常处理
    @ExceptionHandler
    public  AjaxResult error(Exception e){
        return  AjaxResult.error("false",e);
    }
    //设置日期格式
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
        // true:允许输入空值，false:不能为空值
    }

}

